<template>
  <div class="app-container">

    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="6">
            <el-form-item label="教 练 名">
              <el-input v-model="searchObj.coachName" style="width: 95%" placeholder="教练名字" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" icon="el-icon-search" size="mini"
                     :disabled="$hasBP('bnt.coach.list') === false" @click="fetchData()">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" :disabled="$hasBP('bnt.coach.list') === false"
                     @click="resetData">重置</el-button>
        </el-row>
      </el-form>
    </div>

    <!-- 工具条 -->
    <div class="tools-div">
      <el-button type="success" icon="el-icon-plus" size="mini" :disabled="$hasBP('bnt.coach.add') === false"
                 @click="add">添 加</el-button>
    </div>

    <!-- 列表 -->
    <el-table v-loading="listLoading" :data="list" stripe border style="width: 100%;margin-top: 10px;">

      <el-table-column label="序号" width="70" align="center" fixed="left">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="sex" label="头像">
        <template slot-scope="scope">
          <el-avatar :src="scope.row.img" />
        </template>
      </el-table-column>
      <el-table-column prop="coachName" label="教练名" :show-overflow-tooltip="true" />
      <el-table-column prop="sex" label="性别" width="60">
        <template slot-scope="scope">
          {{ scope.row.sex === 0 ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column prop="idCard" label="身份证号" width="160" align="center" />
      <el-table-column prop="phone" label="电话" width="120" align="center" />
      <el-table-column prop="storeName" label="门店" :show-overflow-tooltip="true" width="150" align="center" />
      <el-table-column label="职位状态" width="80" align="center">
        <template slot-scope="scope">
          <el-switch :value="scope.row.status === 1" :disabled="$hasBP('bnt.coach.update') === false"
                     @change="switchStatus(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column prop="skill" label="擅长" align="center" :show-overflow-tooltip="true" width="150" />
      <el-table-column prop="price" label="费用" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="count" label="上课数量" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="createTime" label="创建时间" align="center" width="160" />

      <el-table-column label="操作" align="center" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" title="修改"
                     :disabled="$hasBP('bnt.coach.update') === false" @click="edit(scope.row.id)" />
          <el-button type="danger" icon="el-icon-delete" size="mini" title="删除"
                     :disabled="$hasBP('bnt.coach.remove') === false" @click="removeDataById(scope.row.id)" />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination :current-page="page" :total="total" :page-size="limit"
                   style="padding: 30px 0; text-align: center;" layout="total, prev, pager, next, jumper"
                   @current-change="fetchData" />
  </div>
</template>

<script>
import api from '@/api/content/coach'

export default {
  data() {
    return {
      listLoading: true, // 数据是否正在加载
      list: null, // banner列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      searchObj: {}, // 查询表单对象
      stores: [], // 门店列表

      createTimes: []
    }
  },

  // 生命周期函数：内存准备完毕，页面尚未渲染
  created() {
    console.log('list created......')
    this.fetchData()
  },

  // 生命周期函数：内存准备完毕，页面渲染成功
  mounted() {
    console.log('list mounted......')
  },

  methods: {
    // 加载banner列表数据
    fetchData(page = 1) {
      this.page = page
      api.getPageList(this.page, this.limit, this.searchObj).then(
        response => {
          this.list = response.data.records
          this.total = response.data.total
          // 数据加载并绑定成功
          this.listLoading = false
        }
      )
    },

    // 重置查询表单
    resetData() {
      console.log('重置查询表单')
      this.searchObj = {}
      this.createTimes = []
      this.fetchData()
    },

    // 根据id删除数据
    removeDataById(id) {
      // debugger
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => { // promise
        // 点击确定，远程调用ajax
        return api.removeById(id)
      }).then((response) => {
        this.fetchData(this.page)
        this.$message.success(response.message || '删除成功')
      }).catch(() => {
        this.$message.info('取消删除')
      })
    },

    // 教练是否在职
    switchStatus(row) {
      row.status = row.status === 1 ? 0 : 1
      api.updateStatus(row.id, row.status).then(response => {
        if (response.code) {
          this.$message.success(response.message || '操作成功')
          this.fetchData()
        }
      })
    },

    // 去添加
    add() {
      this.$router.push('/content/coachInfo')
    },
    // 去编辑
    edit(id) {
      this.$router.push({
        path: '/content/coachInfo',
        query: { id: id }
      })
    }
  }
}
</script>
